<?php

class PostingController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';

	/*
	public function filters() {
        return array('rights',);
    }
    */
    
	public function actionView($id)
	{
		
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
    }

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new Posting;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Posting']))
		{
					$model->judul = $_POST['Posting']['judul'];
					$model->deskripsi = $_POST['Posting']['deskripsi'];
					$model->isi = $_POST['Posting']['isi'];
					$model->jenis_id = $_POST['Posting']['jenis_id'];

			$upload_path = getenv("DOCUMENT_ROOT") . '/kejaksaan/images/berita/';

			foreach ($_FILES['image']['name'] as $key => $image) {
            	$nama_dokumen = $image;
                move_uploaded_file($_FILES['image']['tmp_name'][$key], $upload_path . $nama_dokumen);
                $model->image = $nama_dokumen;		
			}

			if($model->save()){
				$this->redirect(array('admin'));
			}
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);
		$currentFile = $model->image;
			
		$source_path = getenv("DOCUMENT_ROOT") . '/kejaksaan/images/berita/'.$currentFile;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Posting']))
		{
			
			$filesUpload = $_FILES['image']['name'];

			foreach ($filesUpload as $key => $image) {
				if(empty($image)){
					$model->judul = $_POST['Posting']['judul'];
					$model->deskripsi = $_POST['Posting']['deskripsi'];
					$model->isi = $_POST['Posting']['isi'];
					$model->jenis_id = $_POST['Posting']['jenis_id'];
					$model->image = $currentFile;

					
					$destination_path = getenv("DOCUMENT_ROOT") . '/kejaksaan/images/berita/'.$currentFile;
					

					if($model->save()){
					if (copy($source_path,$destination_path)) {
  					unlink($source_path);
					}
					$this->redirect(array('admin'));
					}
				}


				if(!empty($image)){
				
				
					$model->judul = $_POST['Posting']['judul'];
					$model->deskripsi = $_POST['Posting']['deskripsi'];
					$model->isi = $_POST['Posting']['isi'];
					$model->jenis_id = $_POST['Posting']['jenis_id'];
					$model->image = $image;
					

					$destination_path = getenv("DOCUMENT_ROOT") . '/kejaksaan/images/berita/';
					

					unlink($source_path);
					if($model->save()){
					move_uploaded_file($_FILES['image']['tmp_name'][$key], $destination_path . $model->image);	
					$this->redirect(array('admin'));
					}

		
				}
				


			}

			
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		if(Yii::app()->request->isPostRequest)
		{
			// we only allow deletion via POST request
			$this->loadModel($id)->delete();

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_GET['ajax']))
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}


	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		 $session=new CHttpSession;
            $session->open();		
            $criteria = new CDbCriteria();            

                $model=new Posting('search');
                $model->unsetAttributes();  // clear any default values

                if(isset($_GET['Posting']))
		{
                        $model->attributes=$_GET['Posting'];
			
			
                   	
                       if (!empty($model->id)) $criteria->addCondition('id = "'.$model->id.'"');
                     
                    	
                       if (!empty($model->judul)) $criteria->addCondition('judul = "'.$model->judul.'"');
                     
                    	
                       if (!empty($model->deskripsi)) $criteria->addCondition('deskripsi = "'.$model->deskripsi.'"');
                     
                    	
                       if (!empty($model->isi)) $criteria->addCondition('isi = "'.$model->isi.'"');
                     
                    	                     
                    	
                       if (!empty($model->image)) $criteria->addCondition('image = "'.$model->image.'"');
                     
                    	
                       if (!empty($model->jenis_id)) $criteria->addCondition('jenis_id = "'.$model->jenis_id.'"');
                     
                    	
                       if (!empty($model->created_by)) $criteria->addCondition('created_by = "'.$model->created_by.'"');
                     
                    	
                       if (!empty($model->created_time)) $criteria->addCondition('created_time = "'.$model->created_time.'"');
                     
                    	
                       if (!empty($model->updated_by)) $criteria->addCondition('updated_by = "'.$model->updated_by.'"');
                     
                    	
                       if (!empty($model->updated_time)) $criteria->addCondition('updated_time = "'.$model->updated_time.'"');
                     
                    			
		}
                 $session['Posting_records']=Posting::model()->findAll($criteria); 
       

                $this->render('admin',array(
			'model'=>$model,
		));

	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Posting::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}



		public function actionList()
        {		
				$criteria=new CDbCriteria;
				$criteria->condition = 'posting=1 order by id desc';
                $dataProvider=new CActiveDataProvider('Posting', array(
				        'criteria'=>$criteria,
                        'pagination'=>array(
                                'pageSize'=>10,
                                'pageVar'=>'page',
                        ),
                        'sort'=>array(
                                //'defaultOrder'=>'created_time',
                        ),
                ));
                $this->render('list',array(
                        'dataProvider'=>$dataProvider,
                ));
        }




	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='posting-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
        
}
